package com.foo.ui_demo_widge.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.foo.ui_demo_widge.R;
import com.foo.ui_demo_widge.pojo.ClassRoomBuilding;

import java.util.ArrayList;

public class ClassRoomBuildingAdapter extends BaseAdapter {
    private Context context;//活动类父类
    private ArrayList<ClassRoomBuilding> data;//数据集合
    private ViewHolder vh = new ViewHolder();//视图托管对象

    public ClassRoomBuildingAdapter(Context context, ArrayList<ClassRoomBuilding> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.classroom_spinner_item, null);
            vh.tv_name = convertView.findViewById(R.id.tv_building_name);
            vh.tv_number = convertView.findViewById(R.id.tv_building_number);
            vh.tv_type = convertView.findViewById(R.id.tv_building_type);
            vh.iv_icon = convertView.findViewById(R.id.iv_building_icon);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();

        }
        if (position != 0) {
            vh.iv_icon.setImageResource(R.drawable.img1);
        }
        ClassRoomBuilding ps = data.get(position);
        vh.tv_name.setText(ps.getName());
        vh.tv_number.setText(ps.getNumber());
        vh.tv_type.setText(ps.getType());
        return convertView;
    }

    //下拉视图托管类
    static class ViewHolder {
        ImageView iv_icon;
        TextView tv_name;
        TextView tv_number;
        TextView tv_type;
    }
}
